package online.xuping.thousandsOfWorlds.sd;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileAppender;
import cn.hutool.core.lang.Dict;
import lombok.SneakyThrows;
import online.xuping.common.IdUtils;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SdWordsMaker {

    /**
     * 创建关键字
     * @param basePrompt
     * @param header
     * @param strings
     * @return
     * @throws IOException
     */
    @SneakyThrows
    protected static List<String> word2List(String basePrompt, String header, String ... strings)  {
        List<String> re = new ArrayList<>();
        //初始化代码
        StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        String[] strings1 = header.split(",");
        List<Dict> src = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            src = xyzWords(strings1[i], strings[i],src);
        }

        for (int i = 0; i < src.size(); i++) {
            Dict dict = src.get(i);
            //获取模板
            Template t = gt.getTemplate(basePrompt);
            t.binding(dict.toBean(HashMap.class));

            String str = t.render();
            re.add(str);
        }
        return re;
    }

    /**
     * 创建关键字，在每个关键字种加入id。
     * @param basePrompt
     * @param header
     * @param strings
     * @return
     * @throws IOException
     */
    protected static List<String> word2ListWithId(String basePrompt,String header,String ... strings)  {
        List<String> re =word2List(basePrompt,header,strings);
        List<String> re1 = new ArrayList<>();
        re.stream().forEach(it->{
            re1.add( IdUtils.newId() + "," +it );
        });
        return re1;
    }
    public static List<Dict> xyzWords(  String key,  String  strings1 , List<Dict> src ){

        List<Dict> re = new ArrayList<>();

        String[] s1 = strings1.split("\\|");
        for (int i = 0; i < s1.length; i++) {
            if (src != null && src.size() > 0) {
                for (int j = 0; j < src.size(); j++) {
                    Dict dict = new Dict(src.get(j));
                    re.add(dict.set(key, s1[i]));
                }
            }else {
                re.add(Dict.create().set(key, s1[i]));
            }
        }
        return re;
    }



}
